package org.bugjail.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Project;
import org.bugjail.data.Suite;
import org.bugjail.data.TestCase;
import org.bugjail.data.TestCaseExecution;
import org.bugjail.data.TestCycle;
import org.bugjail.data.TestDesign;
import org.bugjail.data.TestReport;
import org.bugjail.data.TestRound;
import javax.servlet.Servlet;

/**
 * Servlet implementation class for BasicServlet: BasicUIServlet
 * 
 */
public abstract class BasicUIServlet extends BasicServlet implements Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public BasicUIServlet() {
		super();
	}

	/**
	 * Method to return the title of the page.
	 * 
	 * @return The title of the page.
	 */
	protected abstract String pageTitle();

	protected abstract void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException;

	private final String getPath(TestCase tc) {
		return getPath(tc.getSuite()) + " &gt; " + tc.getName();
	}

	private final String getPath(TestCaseExecution tce) {
		return getPath(tce.getTestRound()) + " &gt; "
				+ tce.getTestCase().getName();
	}

	private final String getPath(Suite s) {
		return getPath(s.getTestDesign()) + " &gt; " + s.getName();
	}

	private final String getPath(TestRound tr) {
		return getPath(tr.getTestCycle()) + " &gt; " + tr.getName();
	}

	private final String getPath(TestCycle tc) {
		return getPath(tc.getTestReport()) + " &gt; " + tc.getName();
	}

	private final String getPath(TestReport tr) {
		return getPath(tr.getProject()) + " &gt; " + tr.getName();
	}

	private final String getPath(TestDesign td) {
		return getPath(td.getProject()) + " &gt; " + td.getName();
	}

	private final String getPath(Project p) {
		return p.getName();
	}

	protected final String showPath(Object o) {
		String path = "";
		if (o instanceof TestCase) {
			path = getPath((TestCase) o);
		} else if (o instanceof TestDesign) {
			path = getPath((TestDesign) o);
		} else if (o instanceof Suite) {
			path = getPath((Suite) o);
		} else if (o instanceof TestCaseExecution) {
			path = getPath((TestCaseExecution) o);
		} else if (o instanceof Project) {
			path = getPath((Project) o);
		} else if (o instanceof TestReport) {
			path = getPath((TestReport) o);
		} else if (o instanceof TestCycle) {
			path = getPath((TestCycle) o);
		} else if (o instanceof TestRound) {
			path = getPath((TestRound) o);
		}
		return "<p class='fontsmall'>" + path + "</p>";
	}

	protected final void action(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw = response.getWriter();
		ServletUtilities.writeTop(pageTitle(), pw, getLoggedUser());
		write(request, response);
		ServletUtilities.writeBottom(pw);
	}

	protected abstract void write(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException;

	public String retrieveAllowedStringFromData(String data) {
		return data;
	}
}